package com.guigu.qingcheng.service_user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.user.TbAddress;
import com.guigu.qingcheng.service_user.service.TbAddressService;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/service_user/tb-address")
public class TbAddressController {

    @Autowired
    private TbAddressService addressService;

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping("/address/findAll")
    public R findAll(){
      List<TbAddress> list= addressService.list(null);
      return R.ok().data("list",list);
    }

    /**
     * 分页查询数据
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/address/findPage")
    public R findPage(@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        Page<TbAddress> page1=new Page<>(page,size);

        addressService.page(page1,null);

        long total=page1.getTotal();
        List<TbAddress> list=page1.getRecords();
        if(list.size()>0){
            return R.ok().data("total",total).data("rows",list);
        }
        return R.error().data("total",total).data("rows",list);
    }

    /**
     * 条件查询数据
     * @param
     * @return
     */
    @PostMapping("/address/findList")
    public R findList(@RequestBody Map<String, Object> searchMap){
        List<TbAddress> list=addressService.findList(searchMap);
        if(list.size()>0){
            return R.ok().data("list",list);
        }
        return R.error().data("list",list);
    }

    /**
     * 条件+分页查询数据
     * @param page
     * @param size
     * @param searchMap
     * @return
     */
    @PostMapping("/address/findPage")
    public R findPage(@RequestParam("page") Integer page,@RequestParam("size") Integer size,@RequestBody(required = false) Map<String,Object> searchMap){
        Page<TbAddress> page1=addressService.findPage(searchMap, page, size);
                long total=page1.getTotal();
                List<TbAddress> list=page1.getRecords();
                if(list.size()>0){
                    return R.ok().data("total",total).data("rows",list);
                }
        return R.error().data("total",total).data("rows",list);
    }

    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/address/findById")
    public R findById(@RequestParam("id") Integer id){
        TbAddress address=addressService.getById(id);
        if(address==null){
            return R.error().data("address",address);
        }
        return R.ok().data("address",address);
    }

    /**
     * 增加数据
     * @param address
     * @return
     */
    @PostMapping("/address/add")
    public R add(@RequestBody TbAddress address){
        address.setIsDefault("0");
        boolean save = addressService.save(address);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 修改数据
     * @param address
     * @return
     */
    @PostMapping("/address/update")
    public R update(@RequestBody TbAddress address){
        boolean flag = addressService.updateById(address);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @GetMapping("/address/delete")
    public R delete(@RequestParam("id") Integer id){
        boolean flag=addressService.removeById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 根据用户名查询地址列表
     * @return
     */
    @RequestMapping("/findAddressList")
    public R findAddressList(@RequestParam("username")String username){
        List<TbAddress> list=addressService.findByUsername(username);
        return R.ok().data("rows",list);
    }

}

